﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_Class;

namespace NLLSite.Administrator.UserControl
{
    public partial class AdminManage : System.Web.UI.UserControl
    {
        Admin ads = new Admin();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("cbxCheck");
                if (CheckBox1.Checked)
                {
                    cb.Checked = true;
                }
                else
                {
                    cb.Checked = false;
                }
            }      
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemAdmin.aspx");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow r in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)r.FindControl("cbxCheck");
                if (cb != null && cb.Checked)
                {
                    lblSuccess.Text = ads.DeleteAdmin(Convert.ToInt32(GridView1.DataKeys[r.RowIndex].Value));
                    lblSuccess.Visible = true;
                }
            }
            GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtID.Text = GridView1.SelectedRow.Cells[1].Text;
            txtTaiKhoan.Text = GridView1.SelectedRow.Cells[2].Text;
             txtMatKhau.Text= GridView1.SelectedRow.Cells[3].Text;
             txtRole.Text = GridView1.SelectedRow.Cells[4].Text;
             txtTen.Text = GridView1.SelectedRow.Cells[5].Text;
             txtDienThoai.Text = GridView1.SelectedRow.Cells[6].Text;
             txtEmail.Text = GridView1.SelectedRow.Cells[7].Text; 
            
            bool status = ((CheckBox)GridView1.SelectedRow.Cells[8].Controls[0]).Checked;
            if (status)
            {
                CheckBox2.Checked = true;
            }
            else
            {
                CheckBox2.Checked = false;
            }
            Panel1.Visible = true;
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("QuanLyAdmin.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            DAL_Class.Administrator ad = new DAL_Class.Administrator();
            bool status = false;
            if (CheckBox2.Checked)
            {
                status = true;
            }
            else
            {
                status = false;
            }
            ad.ID = Convert.ToInt32(txtID.Text);
            ad.Username = txtTaiKhoan.Text;
            ad.Password = "";
            if (txtMatKhau.Text != "") {
                ad.Password = GetMD5Hash(txtMatKhau.Text);
            }
            ad.FullName = txtTen.Text;
            ad.Phone = txtDienThoai.Text;
            ad.Email = txtEmail.Text;
            ad.Status = status;
            lblSuccess.Text = ads.UpdateAdmin(ad);
            GridView1.DataBind();
            Panel1.Visible = false;
        }
        public static string GetMD5Hash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string password = s.ToString();
            return password;
        }
        

        
    }
}